﻿using System;
using System.Collections.Generic;
using System.Text;
using Aga.Controls.Tree;
using System.Xml;
using FlexminAPI;

namespace FlexminAdmin.Tree
{
	internal class XmlTreeSerializer
	{
		public static void Serialize(string fileName, FlexminTreeModel model, TreeViewAdv tree)
		{
			XmlTextWriter xmlWriter = new XmlTextWriter(fileName, Encoding.UTF8);
			xmlWriter.Formatting = Formatting.Indented;
			xmlWriter.WriteStartDocument();
			xmlWriter.WriteStartElement("HostList");
			foreach (Node node in model.Nodes)
			{
				if (node.Text == Properties.Resources.rootNodeName)
				{
					TreeNodeAdv treeNodeAdv = tree.FindNodeByTag(node);
					xmlWriter.WriteAttributeString("expanded", treeNodeAdv.IsExpanded.ToString());
					foreach (Node subNode in node.Nodes)
					{
						WriteNodes(xmlWriter, subNode, tree);
					}
					break;
				}
			}
			xmlWriter.WriteEndElement();
			xmlWriter.WriteEndDocument();
			xmlWriter.Close();
		}


		private static void WriteNodes(XmlTextWriter xmlWriter, Node node, TreeViewAdv tree)
		{
			if (node is GroupTreeNode)
			{
				xmlWriter.WriteStartElement("group");
				xmlWriter.WriteAttributeString("name", node.Text);
				TreeNodeAdv treeNode = tree.FindNodeByTag(node);
				xmlWriter.WriteAttributeString("expanded", treeNode.IsExpanded.ToString());
				foreach (Node subNode in node.Nodes)
					WriteNodes(xmlWriter, subNode, tree);
				xmlWriter.WriteEndElement();

				return;
			}

			if (node is ComputerTreeNode)
			{
				IHost computer = (node as ComputerTreeNode).Host;
				if (computer != null)
				{
					xmlWriter.WriteStartElement("computer");
					xmlWriter.WriteAttributeString("host", computer.HostAddress);
					xmlWriter.WriteAttributeString("password", computer.Password);
					xmlWriter.WriteEndElement();
				}
			}
		}

		public static void Deserialize(string fileName, FlexminTreeModel model, TreeViewAdv tree)
		{
			XmlDocument xmlDoc = new XmlDocument();

			try
			{
				xmlDoc.Load(fileName);
			}
			catch
			{
				return;
			}

			model.Clear();

			if (model.Root.Nodes.Count != 1)
				return;

			Node rootGroup = model.Root.Nodes[0];
			if (rootGroup.Text != Properties.Resources.rootNodeName)
				return;

			XmlNode node = xmlDoc.SelectSingleNode("/HostList");
			TreeNodeAdv treeNodeAdv = tree.FindNodeByTag(rootGroup);
			try
			{
				treeNodeAdv.IsExpanded = (node.Attributes["expanded"].Value == true.ToString());
			}
			catch
			{
				// TODO: Записывать в лог об ошибках в сейве
			}
			FillTreeNode(rootGroup, node, tree);
		}

		private static void FillTreeNode(Node treeNode, XmlNode node, TreeViewAdv tree)
		{
			foreach (XmlNode subNode in node.ChildNodes)
			{
				switch (subNode.Name)
				{
					case "computer":
						try
						{
							ComputerTreeNode computerNode = new ComputerTreeNode(new RemoteComputer(subNode.Attributes["host"].Value, subNode.Attributes["password"].Value));
							treeNode.Nodes.Add(computerNode);
						}
						catch
						{
							// TODO: Записывать в лог об ошибках в сейве
						}
						break;
					case "group":
						try
						{
							GroupTreeNode groupNode = new GroupTreeNode(subNode.Attributes["name"].Value);
							treeNode.Nodes.Add(groupNode);
							try
							{
								TreeNodeAdv treeNodeAdv = tree.FindNodeByTag(groupNode);
								treeNodeAdv.IsExpanded = (subNode.Attributes["expanded"].Value == true.ToString());
							}
							catch
							{
								// TODO: Записывать в лог об ошибках в сейве
							}
							FillTreeNode(groupNode, subNode, tree);
						}
						catch
						{
							// TODO: Записывать в лог об ошибках в сейве
						}
						break;
				}
			}
		}

	}
}
